<?php

class City extends Controller {
	
	function City()
	{
		parent::Controller();
		$this->load->model('City_model');
		$this->lang->load('global_admin','english');
		
	}
	
	function index() {
		$data['inc_script'] = '';
		$data['city_info'] = $this->City_model->getCities();
		$this->load->view('administrator/city_view',$data);
	}
	
	function add()
	{
		//get value from form
		$city_info = array(
			'name' => $this->input->post('tf_name'),
			'url_rewrite' => $this->input->post('tf_url_rewrite'),
			'country_id' => 1,
		);
		
		$form_validation = array(
			array('field' => 'tf_name','label' => 'lang:city_name','rules' => 'required|max_length[45]'),
			array('field' => 'tf_url_rewrite','label' => 'lang:url_rewrite','rules' => 'required|max_length[45]'),
		);
		$this->form_validation->set_rules($form_validation);

		if ($this->form_validation->run() == true) {
			$this->City_model->addCity($city_info);
			redirect('administrator/city');			
		}
		
		$data['inc_script'] = '';
		$this->load->view('administrator/city_add_view', $data);
	}
	
	function edit()
	{
		//get City id from uri
		$id = $this->uri->segment(4);
		
		//get value from form
		$city_info = array(
			'name' => $this->input->post('tf_name'),
			'url_rewrite' => $this->input->post('tf_url_rewrite'),
			'country_id' => 1,
		);
		
		$form_validation = array(
			array('field' => 'tf_name','label' => 'lang:city_name','rules' => 'required|max_length[45]'),
			array('field' => 'tf_url_rewrite','label' => 'lang:url_rewrite','rules' => 'required|max_length[45]'),
		);
		$this->form_validation->set_rules($form_validation);
		
		if ($this->form_validation->run() == true) {
			$this->City_model->updateCity($id,$city_info);
			redirect('administrator/city');			
		}
		
		$data['form_info'] = $this->City_model->getCity(array('id'=>$id));
		$data['inc_script'] = '';
		$this->load->view('administrator/city_edit_view', $data);
	}
	
	function delete()
	{
		$id = $this->uri->segment(4);	
		$this->City_model->deleteCity($id);
		redirect('administrator/city');		
	}
}